﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using PUCIT.eFCS.Utility.Encryption;
using System.Web.Configuration;
using PUCIT.eFCS.Business;

namespace PUCIT.eFCS
{
    public partial class ViewCompletedCirculationList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BusinessClass eFCS = new BusinessClass();
                List<string> forms = eFCS.GetUserForms((int)Session["userId"]);
                string[] details;

                if (forms.Count == 1)
                {
                    details = forms[0].Split(',');
                    string[] parts = details[0].Split('/');
                    DropDownList1.Items.Add(new ListItem(eFCS.GetAbbreviation(parts[0]) + " " + parts[1], details[1]));
                    DropDownList1.Enabled = false; // No need to display filter if there is only one option to select
                }

                else
                {
                    DropDownList1.Items.Add(new ListItem("All", "0"));

                    foreach (string form in forms)
                    {
                        details = form.Split(',');
                        string[] parts = details[0].Split('/');
                        DropDownList1.Items.Add(new ListItem(eFCS.GetAbbreviation(parts[0]) + " " + parts[1], details[1]));
                    }

                    DropDownList1.Text = Request.QueryString["documentId"];
                }
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            HyperLink link = (HyperLink)e.Row.FindControl("link");


            if (link != null) // If its not the first row (headers of grid view)
            {
                NameValueCollection queryStringParameters = new NameValueCollection();
                queryStringParameters.Add("circulationId", e.Row.Cells[0].Text);

                // Encrypt query string parameters
                string encryptedString = CryptoQueryStringHandler.EncryptQueryStrings(queryStringParameters, WebConfigurationManager.AppSettings["CryptoKey"]);

                string base_url = WebConfigurationManager.AppSettings["base_url"];

                link.NavigateUrl = base_url +"ViewStationUserCirculationDetails.aspx?" + encryptedString;
                // btn.Attributes.Add("onClick", "ViewDetails(" + e.Row.Cells[0].Text + ")");
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedValue = DropDownList1.SelectedValue;
            string encryptedString = "";
            NameValueCollection queryStringParameters = new NameValueCollection();

            if (selectedValue.Equals("0")) // All
            {
                queryStringParameters.Add("documentId", "0");
            }
            else
            {
                queryStringParameters.Add("documentId", selectedValue);
                queryStringParameters.Add("disableDocumentFilter", "0");
            }

            // Encrypt query string parameters
            encryptedString = CryptoQueryStringHandler.EncryptQueryStrings(queryStringParameters, WebConfigurationManager.AppSettings["CryptoKey"]);

            Response.Redirect("ViewCompletedCirculationList.aspx?" + encryptedString);
        }
    }
}